package com.csxy.yuekaiyeye.service.impl;
import com.csxy.yuekaiyeye.service.EduCourseDescriptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.yuekaiyeye.entity.EduCourse;
import com.csxy.yuekaiyeye.entity.EduCourseDescription;
import com.csxy.yuekaiyeye.entity.vo.CourseInfoForm;
import com.csxy.yuekaiyeye.mapper.EduCourseMapper;
import com.csxy.yuekaiyeye.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    // 添加课程基本信息
    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
        // 向课程表里面添加课程基本信息
        // CourseInfoForm 对象转换成 EduCourse 对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);

        int result = baseMapper.insert(eduCourse);

//        // 如果添加失败
//        if (result <= 0) {
//            throw new MyException(20001, "添加课程信息失败");
//        }

        // 获取添加之后课程信息的 id
        String cid = eduCourse.getId();

        // 向课程简介表里面添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        // 手动设置描述课程表的 id，与上面的课程信息表 id关联
        eduCourseDescription.setId(cid);
        eduCourseDescriptionService.save(eduCourseDescription);

        return cid;
    }
}